
#include "hal_pin.h"

static pin_struct_st pin_structarray[PIN_INDEX_NUM] =
{
		{0,PB3_PCR},		//方向锁电机控制PWM通信，RX
		{0,PC9_PCR},		//边撑开关输入检测，高电平有效
		{0,PC14_PCR},		//点亮LED指示灯
		{0,PC15_PCR},		//氛围灯电控，高电平有效
		{0,PA2_PCR},		//尾灯电控，高电平有效
		{0,PE0_PCR},		//坐垫锁开关输入检测，高电平有效
		{0,PA1_PCR},		//尾箱锁状态输入检测，高电平有效
		{0,PE1_PCR},		//尾箱锁开关输入检测，高电平有效
		{0,PE8_PCR},		//电动挡风上止点输入检测，低电平有效
		{0,PE9_PCR},		//电动挡风下止点输入检测，低电平有效
		{0,PE10_PCR},		//预留调试串口TX
		{0,PA0_PCR},		//电动挡风霍尔值输入检测，PWM/方波
		{0,PE11_PCR},		//预留调试串口RX
		{0,PC11_PCR},		//控制开关电源电路，高电平有效
		{0,PC10_PCR},		//CAN收发器唤醒脚控制
		{0,PB0_PCR},		//CAN通讯，TX
		{0,PB1_PCR},		//CAN通讯，RX
		{0,PC6_PCR},		//BLE通讯，TX
		{0,PC7_PCR},		//BLE通讯，RX
		{0,PA15_PCR},		//解锁反馈输入检测，低电平有效
		{0,PA14_PCR},		//闭锁反馈输入检测，低电平有效
		{0,PA4_PCR},		//左角灯控制，高电平有效
		{0,PA13_PCR},		//右角灯控制，高电平有效
		{0,PA12_PCR},		//迎宾灯控制，高电平有效
		{0,PB9_PCR},		//氛围灯短路保护ADC输入检测
		{0,PB8_PCR},		//尾灯短路保护ADC输入检测
		{0,PB10_PCR},		//电动挡风_+_-短路保护ADC输入检测
		{0,PD2_PCR},		//位置灯短路保护ADC输入检测
		{0,PD3_PCR},		//左角灯短路保护ADC输入检测
		{0,PD4_PCR},		//右角灯短路保护ADC输入检测
		{0,PD5_PCR},		//迎宾灯短路保护ADC输入检测
		{0,PD8_PCR},		//HSD诊断：ADC采集
		{0,PB4_PCR},		//电源电压ADC采集
		{0,PB6_PCR},		//近光继电器控制短路保护ADC输入检测
		{0,PB7_PCR},		//远光继电器控制短路保护ADC输入检测
		{0,PD9_PCR},		//尾箱锁电控短路保护ADC输入检测
		{0,PD10_PCR},		//油箱盖电控短路保护ADC输入检测
		{0,PD11_PCR},		//坐垫锁电控短路保护ADC输入检测
		{0,PB11_PCR},		//电喇叭驱动短路保护ADC输入检测
		{0,PD12_PCR},		//方向锁电源控制短路保护ADC输入检测
		{0,PB12_PCR},		//位置灯控制，高电平有效
		{0,PD13_PCR},		//HSD通道1输入
		{0,PB13_PCR},		//HSD通道0输入
		{0,PD14_PCR},		//HSD诊断使能脚输入
		{0,PB14_PCR},		//电喇叭驱动，高电平有效
		{0,PD15_PCR},		//油箱盖电控，高电平有效
		{0,PB15_PCR},		//坐垫锁电控，高电平有效
		{0,PA3_PCR},		//电源(钥匙电)继电器控制，短路保护ADC输入检测
		{0,PA7_PCR},		//电源(钥匙电)继电器控制，高电平有效
		{0,PA8_PCR},		//近光继电器控制，高电平有效
		{0,PA10_PCR},		//远光继电器控制，高电平有效
		{0,PA11_PCR},		//方向锁电源控制，输出TTL电平
		{0,PE12_PCR},		//电动挡风_-_+短路保护ADC输入检测
		{0,PC3_PCR},		//电动挡风_-_+短路保护ADC输入检测
		{0,PC2_PCR},		//电动挡风_+_-控制，高电平有效
		{0,PA5_PCR},		//HSD诊断通道选择脚输入
		{0,PA6_PCR},		//尾箱锁电控，高电平有效
		{0,PE2_PCR},		//坐垫箱状态输入检测，高电平有效
		{0,PE3_PCR},		//刹车开关输入检测，高电平有效
		{0,PC5_PCR},		//车辆上电输入检测(旋钮锁钥匙电)，高电平有效
		{0,PC4_PCR},		//自动大灯档输入检测，高电平有效
		{0,PE4_PCR},		//位置灯档输入检测，高电平有效
		{0,PE5_PCR},		//超车灯开关输入检测，高电平有效
		{0,PE6_PCR},		//应急灯开关输入检测，高电平有效
		{0,PE7_PCR},		//右转灯开关输入检测，高电平有效
		{0,PC12_PCR},		//左转灯开关输入检测，高电平有效
		{0,PC13_PCR},		//近光大灯档输入检测，高电平有效
		{0,PC8_PCR},		//远光大灯档输入检测，高电平有效
		{0,PB2_PCR}			//方向锁电机控制PWM通信，TX
};

static result_en hal_setgpio(uint8_t pin,pin_mode_en mode)
{
	result_en result = HAL_FAIL;
	switch(mode)
	{
	case PIN_MODE_IN_FLOAT:
		SIUL_Init(pin,(PCR_IBE));
		result = HAL_SUCCEED;
		break;
	case PIN_MODE_OUT_OPEN_DRAIN:
		SIUL_Init(pin,(PCR_OBE|PCR_ODE));
		result = HAL_SUCCEED;
		break;
	case PIN_MODE_OUT_PUSH_PULL:
		SIUL_Init(pin,(PCR_OBE));
		result = HAL_SUCCEED;
		break;
	case PIN_MODE_IN_DOWN:
		SIUL_Init(pin,(PCR_IBE|PCR_WPE));
		result = HAL_SUCCEED;
		break;
	case PIN_MODE_IN_UP:
		SIUL_Init(pin,(PCR_IBE|PCR_WPE|PCR_WPS));
		result = HAL_SUCCEED;
		break;
	default:
		result = HAL_FAIL;
		break;
	}
	return result;
}

static result_en hal_setadc(uint8_t pin)
{
	SIUL_Init_ADC(pin);
	return HAL_SUCCEED;
}
static result_en hal_setpwm(uint8_t pin , pin_mode_en mode)
{
	result_en result = HAL_FAIL;
	if(PIN_MODE_PWM_IN == mode)
	{
		switch(pin)
		{
		case PA0_PCR:
			SIU.PCR[pin].R = (PCR_PA1|PCR_IBE);
			result = HAL_SUCCEED;
			break;
		default:
			result = HAL_FAIL;
			break;
		}
	}
	else if(PIN_MODE_PWM_OUT == mode)
	{
		switch(pin)
		{
		case PC3_PCR:
			SIU.PCR[pin].R = (PCR_PA1);
			result = HAL_SUCCEED;
			break;
		case PC4_PCR:
			SIU.PCR[pin].R = (PCR_PA1);
			result = HAL_SUCCEED;
			break;
		default:
			result = HAL_FAIL;
			break;
		}
	}
	else
	{
		result = HAL_FAIL;
	}
	return result;
}

static result_en hal_setcan(uint8_t pin , pin_mode_en mode)
{
	result_en result = HAL_FAIL;

	if(PIN_MODE_CAN_TX == mode)
	{
		switch(pin)
		{
		case PB0_PCR:
			SIU.PCR[PB0_PCR].R = (PCR_PA1|PCR_OBE|PCR_SRC);
			break;
		default:
			result = HAL_FAIL;
			break;
		}
	}
	else if(PIN_MODE_CAN_RX == mode)
	{
		switch(pin)
		{
		case PB1_PCR:
			SIU.PCR[PB1_PCR].R = (PCR_IBE);
			break;
		default:
			result = HAL_FAIL;
			break;
		}
	}
	else
	{
		result = HAL_FAIL;
	}
	return result;
}

static result_en hal_setuart(uint8_t pin, pin_mode_en mode)
{
	result_en result = HAL_FAIL;
	if(PIN_MODE_UART_TX == mode)
	{
		switch(pin)
		{
		case PC6_PCR:
			SIU.PCR[PC6_PCR].R = (PCR_PA1);
			result = HAL_SUCCEED;
			break;
		case PB2_PCR:
			SIU.PCR[PB2_PCR].R = (PCR_PA1);
			result = HAL_SUCCEED;
			break;
		default:
			result = HAL_FAIL;
			break;
		}
	}
	else if(PIN_MODE_UART_RX == mode)
	{
		switch(pin)
		{
		case PC7_PCR:
			SIU.PCR[PC7_PCR].R = (PCR_IBE|PCR_WPE|PCR_WPS);
			result = HAL_SUCCEED;
			break;
		default:
			result = HAL_FAIL;
			break;
		}
	}
	else
	{
		result = HAL_FAIL;
	}
	return result;
}

result_en hal_pin_mode(pin_index_en index , pin_mode_en mode)
{
	uint8_t pin = pin_structarray[index].pin;
	result_en result = HAL_FAIL;
	switch(mode)
	{
	case PIN_MODE_IN_FLOAT:
		result = hal_setgpio(pin,mode);			//浮空输入
		break;
	case PIN_MODE_OUT_OPEN_DRAIN:				//开漏输出
		result = hal_setgpio(pin,mode);
		break;
	case PIN_MODE_OUT_PUSH_PULL:				//推挽输出
		result = hal_setgpio(pin,mode);
		break;
	case PIN_MODE_IN_DOWN:						//下拉输入
		result = hal_setgpio(pin,mode);
		break;
	case PIN_MODE_IN_UP:						//上拉输入
		result = hal_setgpio(pin,mode);
		break;
	case PIN_MODE_IN_ANALOG:					//ADC输入
		result = hal_setadc(pin);
		break;
	case PIN_MODE_PWM_IN:						//输入捕获
		result = hal_setpwm(pin,mode);
		break;
	case PIN_MODE_CAN_TX:						//can_tx
		result = hal_setcan(pin,mode);
		break;
	case PIN_MODE_CAN_RX:						//can_rx
		result = hal_setcan(pin,mode);
		break;
	case PIN_MODE_UART_TX:						//uart_rx
		result = hal_setuart(pin,mode);
		break;
	case PIN_MODE_UART_RX:						//uart_rx
		result = hal_setuart(pin,mode);
		break;
	case PIN_MODE_PWM_OUT:						//PWM
		result = hal_setpwm(pin,mode);
		break;
	default:
		result = HAL_FAIL;
		break;
	}
	return result;
}

pin_level_en hal_pin_read(pin_index_en index)
{
	uint8_t pin = pin_structarray[index].pin;
	pin_level_en result = PIN_FAILED;
	if(0 == SIU.GPDI[pin].B.PDI)
	{
		result = PIN_LOW;
	}
	else if(1 == SIU.GPDI[pin].B.PDI)
	{
		result = PIN_HIGH;
	}
	else
	{
		result = PIN_FAILED;
	}
	return result;
}

void hal_pin_write(pin_index_en index , pin_level_en level)
{
	uint8_t pin = pin_structarray[index].pin;
	if(PIN_LOW == level)
	{
		SIU.GPDO[pin].B.PDO = 0;
	}
	else if(PIN_HIGH == level)
	{
		SIU.GPDO[pin].B.PDO = 1;
	}
	else
	{

	}
}

void hal_pin_irq(pin_index_en index , pin_irq_mode_en mode , func cal_bck)
{

}

void hal_pin_irq_enabled(pin_index_en index , enabled_en en)
{

}
